#include <stdio.h>
#include <string.h>
#include "read.h"
#include "theme.h"

//GData* _table = NULL;
//GData *_table;
typedef void(*THEME_FUNC)();

struct table_item {
	char name[20];
	void(*func)();
	void(*initFunc)();
};
struct table {
	struct table_item items[128];
	int size;
};

struct table _table;

void theme_load();
void set_basic_theme();
void init_basic_theme();
void theme_set(const char* name);
void theme_reg(const char*,void(*initFunc)(),void(*func)());

void table_init() {
	_table.size = 0;
}

struct table_item* table_find(const char* name) {
	int i;
	for (i=0;i<_table.size;i++) {
		struct table_item* item = &_table.items[i];
		if (strncmp(item->name,name,20) == 0) {
			// found
			return item;
		}
	}
	return NULL;
}

void table_insert(struct table_item* item) {
	strncpy(_table.items[_table.size].name,item->name,20);
	_table.items[_table.size].func = item->func;
	_table.items[_table.size].initFunc = item->initFunc;
	_table.size++;
}

void table_set(const char* name, void(*initFunc)(), void(*func)()) {
	struct table_item* item = table_find(name);
	if (item != NULL) {
		item->initFunc = initFunc;
		item->func = func;
	}
	else {
		struct table_item item;
		strncpy(item.name,name,20);
		item.initFunc = initFunc;
		item.func = func;
		table_insert(&item);
	}
}

THEME_FUNC table_get(const char* name) {
	struct table_item* item = table_find(name);
	if (item != NULL) return item->func;
	return NULL;
}

void theme_init() {
	table_init();
	theme_reg("Basic",init_basic_theme,set_basic_theme);
	int i;
	for(i=0;i<_table.size;i++) {
		struct table_item item = _table.items[i];
		void(*func)() = item.initFunc;
		if (func != NULL)
			(*func)();
		else
			printf("init not found\n");
//		(*(item.initFunc))();
	}

	theme_set(reader->theme);
}

void theme_destroy() {
}

void theme_load() {
	theme_set(reader->theme);
}

void theme_set(const char* name) {
	void(*func)() = table_get(name);
	if (func != NULL) {
		(*func)();
	}
}

void theme_reg(const char* name,void(*initFunc)(),void(*func)()) {
	table_set(name,initFunc,func);
}

void init_basic_theme() {
//	GtkTextBuffer* buffer = gtk_text_view_get_buffer(frame->view);
//	gtk_text_buffer_create_tag(buffer,"basic","background","#CCCCCC");
//	gtk_text_buffer_create_tag(buffer,"basic","foreground","#ffffff");
}

void set_basic_theme() {
//	GtkTextIter start, end;
//	GtkTextBuffer* buffer = gtk_text_view_get_buffer(frame->view);
//	gtk_text_buffer_get_iter_at_offset(buffer,&start,0);
//	gtk_text_buffer_get_iter_at_offset(buffer,&end,-1);
//	gtk_text_buffer_apply_tag_by_name (buffer, "basic", &start, &end);

	GdkColor gray;
	gdk_color_parse("#bbbbbb",&gray);
	gtk_widget_modify_base(GTK_WIDGET(frame->view),GTK_STATE_ACTIVE,&gray);
	gtk_widget_modify_base(GTK_WIDGET(frame->view),GTK_STATE_NORMAL,&gray);
	gtk_widget_modify_base(GTK_WIDGET(frame->view),GTK_STATE_PRELIGHT,&gray);
	gtk_widget_modify_base(GTK_WIDGET(frame->view),GTK_STATE_SELECTED,&gray);
	gtk_widget_modify_base(GTK_WIDGET(frame->view),GTK_STATE_INSENSITIVE,&gray);
}
